package cn.blunce.weather.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.blunce.weather.R;
import cn.blunce.weather.base.AppDB;
import cn.blunce.weather.models.CityID;
import cn.blunce.weather.models.WeatherID;
import cn.blunce.weather.utils.GetWeatherData;
import cn.blunce.weather.utils.ParseUtils;
import cn.blunce.weather.utils.Urls;
import cn.blunce.weather.utils.Utils;

public class InitalActivity extends Activity implements View.OnClickListener {

    private ProgressBar mPB_progressBar;
    private TextView mTV_info;
    private Button mBtn_next;

    private AppDB mAppDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_inital);

        mAppDB = AppDB.getInstance(this);
        initView();
        if (Utils.isExistWeatherID_CitiesID(this)) {
            startActivity(new Intent(InitalActivity.this, WeatherShowActivity.class));
            finish();
        } else {
            new AsyncTask<String[], Void, String>() {
                @Override
                protected String doInBackground(String[]... params) {
                    List<WeatherID> weatherIDs;
                    List<CityID> cityIDs;

                    String weathers = GetWeatherData.request(params[0][1]);
                    String cities = GetWeatherData.request(params[0][0]);

                    try {
                        weatherIDs = ParseUtils.parseWeathers(weathers);
                        cityIDs = ParseUtils.parseCities(cities);
                    } catch (Exception e) {
                        e.printStackTrace();
                        weatherIDs = new ArrayList<>();
                        cityIDs = new ArrayList<>();
                    }

                    try {
                        for (WeatherID weather : weatherIDs)
                            mAppDB.add(weather);
                        for (CityID cityID : cityIDs)
                            mAppDB.add(cityID);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String s) {
                    mPB_progressBar.setVisibility(View.INVISIBLE);
                    mTV_info.setText(R.string.info_downloaded_cities_weathers);
                    mBtn_next.setTextColor(Color.BLACK);
                    mBtn_next.setEnabled(true);
                }
            }.execute(new String[]{Urls.getUrl(Urls.URL_INDEX_WEATHER_IDS, ""),
                    Urls.getUrl(Urls.URL_INDEX_CITY_IDS, "")});
        }
    }

    private void initView() {
        mPB_progressBar = (ProgressBar) findViewById(R.id.PB_progressBar);
        mTV_info = (TextView) findViewById(R.id.tv_info);
        mBtn_next = (Button) findViewById(R.id.btn_next);
        mBtn_next.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                startActivity(new Intent(this, WeatherShowActivity.class));
                finish();
                break;
            default:

        }
    }
}
